import javassist.ClassPool;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;

public class SampleAgent {

//    public static void premain(String agentOps, Instrumentation inst) {
//        System.out.println("=========premain方法执行========");
//        System.out.println(agentOps);
//        // 添加Transformer
//        inst.addTransformer(new PatternTransformer());
//
//    }

    public static void agentmain(String agentOps, Instrumentation inst) throws NoSuchMethodException {
        // 添加Transformer
        System.out.println("agentmain");
        inst.addTransformer(new PatternTransformer(), true);
        for (Class<?> c : inst.getAllLoadedClasses()) {
//            System.out.println(c);

//            if (c.getName().contains("org.apache.catalina.core.ApplicationFilterChain")) {
//                System.out.println("org.apache.catalina.core.ApplicationFilterChain");
//                System.out.println(c);
//
//                Method method = c.getDeclaredMethod("internalDoFilter");
//                method.setAccessible(true);
//                System.out.println(method.toGenericString());
//            }


            if (c.getName().equals("DemoServlet")) {
                if (inst.isModifiableClass(c)) {
                    System.out.println("可以重写类cName:" + c.getName());
                    try {
                        inst.retransformClasses(c);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } else {
                    System.out.println("不可以重写类cName:" + c.getName());
                }

            }
        }


    }


    public static void premain(String agentOps) {
        System.out.println("=========premain方法执行========");
    }
}
